package com.hjq.www.Dao.Impl;

import com.hjq.www.Dao.BreederDao;
import com.hjq.www.utils.JDBCUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.Vector;

/**
 * Create with IntelliJ IDEA
 *
 * @author: hjq
 * @Date:
 * @Description:
 */
public class BreederDaoImpl implements BreederDao {
    Connection conn = null;
    ResultSet rs = null;
    PreparedStatement ps = null;
    DefaultTableModel dtm=null;
    private JTextField idField;
    private JTextField nameField;
    private JTextField passwordField;
    private JTextField genderField;
    private JTextField phoneField;
    private JTextField cardField;

    /**
     * 添加饲养员信息
     * @param idField
     * @param nameField
     * @param passwordField
     * @param genderField
     * @param phoneField
     * @param cardField
     */
    @Override
    public void add(JTextField idField, JTextField nameField, JTextField passwordField, JTextField genderField, JTextField phoneField, JTextField cardField) {
        String id=idField.getText();
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into breeder(id,username,password,gender,phone,idnum) value(?,?,?,?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, idField.getText());
            ps.setString(2, nameField.getText());
            ps.setString(3, passwordField.getText());
            ps.setString(4, genderField.getText());
            ps.setString(5, phoneField.getText());
            ps.setString(6, cardField.getText());
            ps.executeUpdate();
            conn.commit();
            JOptionPane.showMessageDialog(null, "添加成功");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "添加失败");
            e.printStackTrace();
        }
    }

    @Override
    public void search(JTextField idField,JTextField nameField,JTextField passwordField,JTextField genderField,JTextField phoneField,JTextField cardField) {
        String sid=idField.getText();
        boolean flag=false;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "select * from breeder where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,sid);
            rs = ps.executeQuery();
            while(rs.next()){
                String ssid = rs.getString("id");
                if(sid.equals(ssid)){
                    idField.setText(rs.getString("id"));
                    nameField.setText(rs.getString("username"));
                    passwordField.setText(rs.getString("password"));
                    genderField.setText(rs.getString("gender"));
                    phoneField.setText(rs.getString("phone"));
                    cardField.setText(rs.getString("idnum"));
                    flag=true;
                }
            }
            if(flag) {
                JOptionPane.showMessageDialog(null, "查询成功");
            }else{JOptionPane.showMessageDialog(null,"查询失败");}
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"查询失败");
            e.printStackTrace();
        }finally{
            JDBCUtils.close(conn,ps,rs);
        }
    }

    @Override
    public void update(JTextField idField,JTextField nameField,JTextField passwordField,JTextField genderField,JTextField phoneField,JTextField cardField) {
        String id = idField.getText();
        String name = nameField.getText();
        String password = passwordField.getText();
        String gender = genderField.getText();
        String phone = phoneField.getText();
        String card = cardField.getText();
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "update breeder set id=?,username=?,password=?,gender=?,phone=?,idnum=? where id=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            ps.setString(2, name);
            ps.setString(3, password);
            ps.setString(4, gender);
            ps.setString(5, phone);
            ps.setString(6, card);
            ps.setString(7,id);
            ps.executeUpdate();
            if(ps.executeUpdate()!=0) {
                JOptionPane.showMessageDialog(null, "修改成功");
            }else{JOptionPane.showMessageDialog(null, "修改失败");}
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"修改失败");
            e.printStackTrace();

        } finally {
            JDBCUtils.close(conn,ps);
        }
    }

    @Override
    public void delete(JTextField idField) {
        String sno = idField.getText();
        try {
            conn = JDBCUtils.getConnection();
            String sql = "delete from breeder where id =?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,sno);
            ps.execute();
            JOptionPane.showMessageDialog(null,"删除成功");
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null,"删除失败");
            e.printStackTrace();
        } finally {
            JDBCUtils.close(conn,ps);
        }
    }
    @Override
    public void reset(JTextField idField, JTextField nameField, JTextField passwordField, JTextField genderField, JTextField phoneField, JTextField cardField) {
        idField.setText("");
        nameField.setText("");
        passwordField.setText("");
        genderField.setText("");
        phoneField.setText("");
        cardField.setText("");
    }
}
